package com.example.edu.Service.ipml;

import cn.hutool.json.JSONUtil;
import com.example.edu.Dto.LoginDto;
import com.example.edu.Dto.LoginReturnDto;
import com.example.edu.Dto.RegisterDto;
import com.example.edu.Dto.Result;
import com.example.edu.Entity.User;
import com.example.edu.Mapper.UserMapper;
import com.example.edu.Service.UserService;
import com.example.edu.Utils.CacheDataBaseUtil;
import com.example.edu.Utils.JwtUtil;
import com.example.edu.Utils.RegexUtils;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.DefaultTransactionDefinition;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

import static com.example.edu.Utils.Constants.*;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private PlatformTransactionManager transactionManager; // 事务管理器
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private UserMapper userMapper;
    @Resource
    private CacheDataBaseUtil cacheDataBaseUtil;

    @Override
    public Result login(LoginDto loginData) {
        String telephone = loginData.getTelephone();
        String password = loginData.getPassword();
        // 检查手机号
        if(RegexUtils.isPhoneInvalid(telephone)){
            return Result.fail("无效的手机号");
        }
        User user = cacheDataBaseUtil.selectById(USER_KEY,telephone,User.class,telephone2 -> userMapper.getUser(telephone2),USER_CACHE_TTL,TimeUnit.HOURS);
        if(user == null){
            return Result.fail("未注册的用户");
        }
        if(!password.equals(user.getPassword())){
            return Result.fail("密码错误");
        }
        // 生成token
        String token = JwtUtil.generateToken(telephone);
        // 获取角色
        String role = user.getRole();
        LoginReturnDto loginReturn = new LoginReturnDto(token,role);
        return Result.ok(loginReturn);
    }

    @Override
    public Result register(RegisterDto registerData) {
        String telephone = registerData.getTelephone();
        // 检查手机号
        if(RegexUtils.isPhoneInvalid(telephone)){
            return Result.fail("无效的手机号");
        }
        if(!cacheDataBaseUtil.isContainsById(USER_KEY,telephone, telephone2 -> userMapper.getUser(telephone2),USER_CACHE_TTL,TimeUnit.HOURS)){
            try {
                User new_user = new User(telephone, registerData.getPassword(), registerData.getNik_name(), registerData.getRole());
                cacheDataBaseUtil.insert(USER_KEY, telephone, new_user, new_user2 -> userMapper.addUser(new_user2), USER_CACHE_TTL, TimeUnit.HOURS);
                return Result.ok();
            } catch (Exception e){
                return Result.fail("注册失败");
            }
        }
        return Result.fail("请勿重复注册");
    }

}
